package com.chainyoung.component_tmtrade.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmtrade.bean.TradeCurrencyItem;
import com.chainyoung.component_tmtrade.ui.mvp.contract.TradeCurrencyListContract;

import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

@ActivityScope
public class TradeCurrencyRecordListPresenter extends BasePresenter<TradeCurrencyListContract.Model, TradeCurrencyListContract.View> {


    @Inject
    public TradeCurrencyRecordListPresenter(TradeCurrencyListContract.Model model, TradeCurrencyListContract.View rootView) {
        super(model, rootView);
    }


    public void getExchangeList(TreeMap<String, Object> map) {
        addSubscribe(mModel.getExchangeTradeList(map)
                .compose(RxUtils.<CommonList<TradeCurrencyItem>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CommonList<TradeCurrencyItem>>(mView) {
                    @Override
                    public void onNext(CommonList<TradeCurrencyItem> data) {
                        mView.onGetTradeListSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetTradeListSuccess(null);
                                return;
                            }
                        }
                        mView.onGetTradeListFailed();
                        super.onError(e);
                    }
                }));
    }


}

